package local_database;

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XmlWriter {
	DocumentBuilderFactory builderFactory;
	DocumentBuilder builder = null;
	Document doc = null;

	String file;

	public XmlWriter(String file) {
		this.file = file;

		builderFactory = DocumentBuilderFactory.newInstance();

		try {
			builder = builderFactory.newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}

	}

	public boolean createRoot() {
		doc = builder.newDocument();
		Element rootElement = doc.createElement("Statistic");
		doc.appendChild(rootElement);
		return writeContent();
	}

	public boolean AddNewElementToRoot(String elementName,
			String attributeName, String attributeValue) {
		try {
			doc = builder.parse(file);
		} catch (Exception e) {
		}

		String childNodeAttr;
		Node childNode = null;
		NamedNodeMap attrm = null;

		NodeList childNodes = doc.getElementsByTagName(elementName);
		for (int i = 0; i < childNodes.getLength(); i++) {
			childNode = doc.getElementsByTagName(elementName).item(i);
			if (childNode != null) {
				attrm = childNode.getAttributes();
				Node nodeAttr = attrm.getNamedItem(attributeName);
				childNodeAttr = nodeAttr.getTextContent();
				if (childNodeAttr.equals(attributeValue)) {
					return false;
				}
			}
		}

		Node stat = doc.getFirstChild();
		Element element = doc.createElement(elementName);
		stat.appendChild(element);

		Attr attr = doc.createAttribute(attributeName);
		attr.setValue(attributeValue);
		element.setAttributeNode(attr);

		return writeContent();
	}

	public boolean AddNewElement(String parentTag, String elementName,
			String content) {
		try {
			doc = builder.parse(file);
		} catch (Exception e) {
			return false;
		}
		NodeList parentList = doc.getElementsByTagName(parentTag);
		Node parent;
		for (int i = 0; i < parentList.getLength(); i++) {
			parent = parentList.item(i);
			Element element = doc.createElement(elementName);
			element.appendChild(doc.createTextNode(content));
			parent.appendChild(element);
		}

		return writeContent();
	}

	public boolean AddNewElement(String parentTag, String parentAttr,
			String parentAttrValue, String elementName, String elementContent) {
		try {
			doc = builder.parse(file);
		} catch (Exception e) {
			return false;
		}
		NodeList parentList = doc.getElementsByTagName(parentTag);
		Node parent;
		Node attrNode;
		boolean isNewElement = true;
		for (int i = 0; i < parentList.getLength(); i++) {
			parent = parentList.item(i);
			attrNode = parent.getAttributes().getNamedItem(parentAttr);

			if ((attrNode.getTextContent()).equals(parentAttrValue)) {

				NodeList list = parent.getChildNodes();
				for (int y = 0; y < list.getLength(); y++) {
					Node node = list.item(y);

					if (elementName.equals(node.getNodeName())) {
						node.setTextContent(elementContent);
						isNewElement = false;
					}
				}

				if (isNewElement) {
					Element element = doc.createElement(elementName);
					element.appendChild(doc.createTextNode(elementContent));
					parent.appendChild(element);
				}
			}
		}
		return writeContent();
	}

	private boolean writeContent() {
		boolean flag = true;

		TransformerFactory transformerFactory = TransformerFactory
				.newInstance();
		Transformer transformer = null;
		try {
			transformer = transformerFactory.newTransformer();

			DOMSource source = new DOMSource(doc);
			StreamResult result = new StreamResult(new File(file));

			transformer.transform(source, result);
		} catch (TransformerException e) {
			e.printStackTrace();
			flag = false;
		}

		return flag;
	}

}
